package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;

    //扩展方法:保存套餐数据,以及套餐菜品关联数据
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //1.添加套餐数据:操作setmeal表,添加一条数据
        //保存之前,id是没有数据的,保存之后,id有值
        this.save(setmealDto);

        //2.添加套餐菜品关联数据,操作Setmeal_dish表,添加多条数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //TODO:给每个SetmealDish设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //批量保存
        setmealDishService.saveBatch(setmealDishes);


    }

    //扩展方法:分页条件查询(套餐信息,分类名称)
    @Override
    public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize, String name) {

        Page<SetmealDto> pageInfo = new Page<>(page,pageSize);

        return setmealMapper.pageSetmealDto(pageInfo,name);
    }

    //扩展方法:要求1:有套餐正在售卖,不能删除 要求2:删除套餐,删除套餐菜品关联数据
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //要求1:有套餐正在售卖,不能删除
        // select count(*) from setmeal where id in (ids) and status=1;
        Integer count = this.lambdaQuery()
                .in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1)
                .count();
        if (count>0){
            throw new CustomerException("有套餐正在售卖,不能删除");
        }

        //要求2:删除套餐,删除套餐菜品关联数据
        //delete from setmeal where id in (ids);
        this.removeByIds(Arrays.asList(ids));
        //delete from setmeal_dish where setmeal_id in (ids);
        setmealDishService.lambdaUpdate()
                .in(SetmealDish::getSetmealId,ids)
                .remove();
    }

    //扩展方法:根据id查询套餐详细信息(包含菜品信息)
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        return setmealMapper.getByIdWithDish(id);
    }

    //扩展方法:更新套餐数据,以及套餐菜品关联数据
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //1.更新套餐数据,操作setmeal表,添加一条数据
        this.updateById(setmealDto);

        //2.更新套餐菜品关联数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //delete from setmeal_dish where setmeal_id=?
        setmealDishService.lambdaUpdate()
                .eq(SetmealDish::getSetmealId,setmealDto.getId())
                .remove();
        //TODO:给每个SetmealDish设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //批量保存
        setmealDishService.saveBatch(setmealDishes);

    }
}
